package com.grm.sys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.grm.common.entity.Result;
import com.grm.common.annotation.HasPerm;
import com.grm.common.annotation.Log;
import com.grm.sys.model.SysRole;
import com.grm.sys.request.role.SysRoleAddParam;
import com.grm.sys.request.role.SysRoleQueryParam;
import com.grm.sys.request.role.SysRoleUpdateParam;
import com.grm.sys.response.SysRoleResponse;
import com.grm.sys.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys/role")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 查询所有角色
     */
    @GetMapping("/query/all")
    public Result queryAll() {
        List<SysRole> list = sysRoleService.queryAll();
        return Result.success(list);
    }

    /**
     * 查询角色列表
     */
    @PostMapping("/list")
    public Result queryListSelective(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestBody SysRoleQueryParam queryParam) {
        // 开启分页
        PageHelper.startPage(pageNum, pageSize);
        List<SysRole> list = sysRoleService.queryListSelective(queryParam);
        PageInfo<SysRole> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 添加角色
     */
    @Log("添加角色")
    @HasPerm("sys:role:add")
    @PostMapping("/add")
    public Result add(@RequestBody SysRoleAddParam sysRoleAddParam) {
        sysRoleService.add(sysRoleAddParam);
        return Result.success();
    }

    /**
     * 修改角色
     */
    @Log("修改角色")
    @HasPerm("sys:role:update")
    @PutMapping("/update")
    public Result update(@RequestBody SysRoleUpdateParam sysRoleUpdateParam) {
        sysRoleService.update(sysRoleUpdateParam);
        return Result.success();
    }


    /**
     * 删除角色
     */
    @Log("删除角色")
    @HasPerm("sys:role:delete")
    @PostMapping("/delete")
    public Result delete(@RequestBody List<Long> ids) {
        sysRoleService.delete(ids);
        return Result.success();
    }
}
